﻿CREATE proc thuan_dangnhap        
@username as varchar(30) , 
@PassWord as nvarchar(50) ,     
@MaChuongTrinh int,  
@IDViTri int     
as        



declare @bangtam table
(
UserID int,
MaNV nvarchar(200),
TenNV nvarchar(200),
DoUuTienUser int,
MaPB varchar(50),
TenPB nvarchar(255),
MaChucVu int,
ChucVu nvarchar(255),
MaNhomUser int,
DoUuTienNhom int,
MaChuongTrinh int,
IDViTri int,
MaNhomMenuND int
)
   

insert into @bangtam (UserID,MaNV,TenNV,DoUuTienUser,MaPB,TenPB,MaChucVu,ChucVu,MaNhomUser,DoUuTienNhom,MaChuongTrinh,IDViTri,MaNhomMenuND)
select a.UserID,a.MaNV,a.TenNV,a.DoUuTien as DoUuTienUser,a.MaPB,f.TenPB,a.MaChucVu,e.ChucVu,c.MaNhomUser,c.DoUuTien as DoUuTienNhom,d.MaChuongTrinh,d.IDViTri,d.MaNhomMenuND
 from DanhMuc_NguoiDung a,QuanTri_PhanNhomNguoiDung b,DanhMuc_NhomNguoiDung c,QuanTri_CapQuyenNguoiDungSuDungMenu d,DanhMuc_ChucVu e,DanhMuc_PhongBan f
 Where a.UserID=b.UserID and b.MaNhomUser=c.MaNhomUser and c.MaNhomUser=d.MaNhomUser and a.MaChucVu=e.MaChucVu and a.MaPB=f.MaPB
 and a.MaNV=@username and  a.MatKhau=CAST(@PassWord as varbinary(50))   and d.MaChuongTrinh=@MaChuongTrinh and d.IDViTri=@IDViTri
 
 select UserID,MaNV,TenNV,DoUuTienUser,MaPB,TenPB,MaChucVu,ChucVu,MaNhomUser,DoUuTienNhom,MaChuongTrinh,IDViTri,MaNhomMenuND from @bangtam
 Where DoUuTienNhom= (select MIN(DoUuTienNhom) from @bangtam)
 


 